<?php


//消息中心数据
namespace App\Http\Controllers\Api\V1\Frontend\News;


use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Http\Resources\MsgInfoResource;
use App\Http\Resources\RsMsgManageResource;
use App\Jobs\PmsWebservice;
use App\Models\Article;
use App\Models\Robot;
use App\Models\RsMsginfo;
use App\Models\RsMsgManage;
use App\Services\ProcessService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class IndexController  extends BaseController {

    //消息列表展示

    public function index(Request $request){
        //验证所需参数是否存在

       // var_dump("asadf");
    }


    //查询自定义消息类别信息
    public function zdType(ProcessService $processService){

        //return  $this->successful($a,'success');
        if($this->userinfo()){
            $user_id=$this->userinfo()->id;
        }else{
            //return $this->failed('登陆失效');
            $user_id=1;

        }
        $data=RsMsgManage::getType($user_id);

        $res=[
            'zdType'=>$data,
            'gdType'=>$processService->getServiceNumber()
        ];
        if($data){
            return $this->successful($res,'success');
        }
        return $this->failed('查询失败');
    }


    //调用相应的服务单列表数据
    public function  ServiceList(Request $request,ProcessService $processService){
        $numkey=$request->post('key');  //传不同服务单索引值
        $page=$request->post('page');  //页码
        $search=$request->post('search',false); //搜索内容
        $date=$request->post('date',false);
        //验证参数是否正确
        $messages = [
            'key.required' => '缺少索引值',
        ];
        $validate = Validator::make($request->all(), [
            'key'=>'required|int',   //消息序号

        ],$messages);
        //判断是否参数完整
        if ($validate->fails()){
            return $this->failed($validate->errors()->first());

        }
        $arrays=[
            'type'=>$numkey,
        ];
        if($search){
            $arrays['search']=$search;
        }
        if($date){
            $arrays['date']=$date;
        }
        $list=$processService->getServiceList($arrays);
        return $list;

    }
    //获取顶级消息分类
    public function zdTypes(Request $request){
        $arrays=['id'=>0,'msg_group'=>'流程消息'];
        $groupData=RsMsgManage::query()->select('id','msg_group')->where('status','=',1)->groupBy('msg_group')->get()->toArray();
        $arr = [];
        $arr[0]['id'] = '0';
        $arr[0]['msg_group'] = '流程消息';
        array_walk($arr,function($item) use (&$groupData) {
            array_unshift($groupData, $item);
        });

        foreach ($groupData as $k=>$v){
            if($v['id']==0){
                $groupData[$k]['data']=array(
                    ['id'=>1,'msg_type'=>'待办'],
                    ['id'=>2,'msg_type'=>'待阅'],
                    ['id'=>3,'msg_type'=>'参与'],
                    ['id'=>4,'msg_type'=>'办结'],
                    ['id'=>5,'msg_type'=>'已阅'],
                    ['id'=>6,'msg_type'=>'团队服务单'],
                );
            }else{
                $req=RsMsgManage::query()->select('id','msg_type')->where('msg_group','=',$v['msg_group'])->where('status','=',1)->get();
                $groupData[$k]['data']=$req;
            }

        }
        return $this->successful($groupData,'success');

    }



    //查询当前消息小列表数据
    public function zdList(Request $request){
        if($this->userinfo()){
            $user_id=$this->userinfo()->id;
        }else{
            //return $this->failed('登陆失效');
            $user_id=1;

        }
        $type_id=$request->post('type_id');

        //验证参数是否正确
        $messages = [
            'type_id.required' => '缺少类别id',
            // 'shop_id.required' => '缺少店铺信息'
        ];
        $validate = Validator::make($request->all(), [
            'type_id'=>'required|int',   //消息序号

        ],$messages);
        //判断是否参数完整
        if ($validate->fails()){
            return $this->failed($validate->errors()->first());

        }

        $content=$request->post('title',false);  //标题
        $daytime=$request->post('daytime',false); //时间
        $page=$request->post('page',1);
        $pagesize=$request->post('pagesize',10);
        $param=[
            'user_id'=>$user_id,
            'content'=>$content,
            'daytime'=>$daytime,
            'type_id'=>$type_id
        ];

        //按照一级目录进行分组
       $data=RsMsgManage::getMsg($page,$pagesize,$param);
        return $this->successful($data,'success');
    }

    //点击查询消息详情
    public function zdInfo(Request $request){
        $id=$request->post('msg_id');  //消息id
        //验证参数是否正确
        $messages = [
            'msg_id.required' => '缺少消息序号',
           // 'shop_id.required' => '缺少店铺信息'
        ];
        $validate = Validator::make($request->all(), [
            'msg_id'=>'required|string',   //消息序号

        ],$messages);
        //判断是否参数完整
        if ($validate->fails()){
            return $this->failed($validate->errors()->first());

        }
        //查询相关信息
        $data=RsMsginfo::query()->where(['id'=>$id])->get();
        $datas=MsgInfoResource::collection($data);
        //将数据更新状态
        if($data) {
            // $date=date('Y-m-d h:i:s',time());
            if ($data[0]['msg_type_id'] == 5) {
                RsMsginfo::query()->where(['id' => $id])->update(['is_read' => 1, 'msg_type_id'=>6, 'read_date' => Carbon::now()->toDateTimeString()]);
            }elseif ($data[0]['msg_type_id'] == 5 ){
                RsMsginfo::query()->where(['id' => $id])->update(['is_read' => 1, 'msg_type_id'=>4, 'read_date' => Carbon::now()->toDateTimeString()]);
            }
        }
        return $this->successful($datas,'success');

    }


    //供第三方使用接口 start
    //消息类别
    public function msgType(Request $request){
        $groupData=RsMsgManage::query()->select('id','msg_group')->where('status','=',1)->groupBy('msg_group')->get()->toArray();
        foreach ($groupData as $k=>$v){
            $req=RsMsgManage::query()->select('id','msg_type')->where('msg_group','=',$v['msg_group'])->where('status','=',1)->get();

            $groupData[$k]['msg_type_list']=$req;

        }
        return $this->successful($groupData,'success');

    }
    //消息内容存储
    public function msgCreate(Request $request){
        $messages = [
            'msg_type_id.required' => '消息类型id错误',
            'msg_title.required' => '消息标题错误',
            'msg_content.required' => '消息内容错误',
            'send_id.required' => '发送人id错误',
            'recevier_id.required' => '接收人id错误',
            // 'shop_id.required' => '缺少店铺信息'
        ];
        $validate = Validator::make($request->all(), [
            'msg_type_id'=>'required|int',   //消息序号
            'msg_title'=>'required|string',   //消息序号
            'msg_content'=>'required|string',   //消息序号
            'send_id'=>'required|int',   //消息序号
            'recevier_id'=>'required|int',   //消息序号

        ],$messages);
        //判断是否参数完整
        if ($validate->fails()){
            return $this->failed($validate->errors()->first());

        }
        //将数据进行存储参数
        $data=RsMsginfo::msgCreated($request->post());
        if($data){
          return  $this->message('添加成功');
        }else{
            return $this->failed('添加失败');
        }
    }
    //end

    //获取推送消息详情
    public function articleInfo(Request $request){
        $messages = [
            'article_sn.required' => '消息编码错误',
            // 'shop_id.required' => '缺少店铺信息'
        ];
        $validate = Validator::make($request->all(), [

            'article_sn'=>'required|string',   //消息序号

        ],$messages);
        //判断是否参数完整
        if ($validate->fails()){
            return $this->failed($validate->errors()->first());

        }
        $data=Article::query()->select('content')->where(['article_sn'=>$request->post('article_sn')])->first();
        if($data){
            return $this->successful($data,'success');

        }else{
           return $this->failed("暂无数据");
        }
    }

}




